<template>
    <div class="container">
        <div class="title">
            <span>修改</span>
            <span class="back" @click="back">
        <i class="iconfont icon-fanhui"></i> 返回
      </span>
        </div>
        <el-divider></el-divider>
        <br/>
        <el-form label-width="140px"
                 :model="form"
                 status-icon
                 ref="form"
                 @submit.native.prevent>
            <el-form-item label="名称" prop="name">
                <el-input size="medium" v-model="form.name" placeholder="请填写名称"></el-input>
            </el-form-item>
            <el-form-item label="金额" prop="price">
                <el-input size="medium" type="number" v-model="form.price" placeholder="请填写金额"></el-input>
            </el-form-item>
            <el-form-item label="邀请返利" prop="discount">
                <el-input size="medium" type="number" v-model="form.discount" placeholder="请填写邀请返利(元)"></el-input>
            </el-form-item>
            <el-form-item label="省级返利" prop="province_discount">
                <el-input size="medium" type="number" v-model="form.province_discount" placeholder="请填写省级返利(元)"></el-input>
                （会员再升级为省级代理返佣金额）
            </el-form-item>
            <el-form-item label="市级返利" prop="city_discount">
                <el-input size="medium" type="number" v-model="form.city_discount" placeholder="请填写市级返利(元)"></el-input>
                （会员再升级为市级代理返佣金额）
            </el-form-item>
            <el-form-item label="区/县级返利" prop="county_discount">
                <el-input size="medium" type="number" v-model="form.county_discount" placeholder="请填写区/县级返利(元)"></el-input>
                （会员再升级为区/县级代理返佣金额）
            </el-form-item>
            <el-form-item label="时间" prop="status">
                <el-select  size="medium" filterable v-model="form.status" placeholder="请选择" clearable>
                    <el-option
                            v-for="(items, index) in statusArr"
                            :key="index"
                            :label="items.name"
                            :value="items.id">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item class="submit">
                <el-button type="primary" @click="submitForm()">保 存</el-button>
                <el-button @click="resetForm('form')">重 置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import cost from '@/models/cost'

export default {
  props: {
    costId: {
      type: Number,
    },
  },
  data() {
    return {
      statusArr: [
        { id: 1, name: '一天会员' },
        { id: 2, name: '一周会员' },
        { id: 3, name: '一个月会员' },
        { id: 4, name: '半年会员' },
        { id: 5, name: '一年会员' },
        { id: 6, name: '两年会员' },
        { id: 7, name: '三年会员' },
      ],
      form: {
        name: '',
        price: '',
        discount: '',
        province_discount: '',
        city_discount: '',
        county_discount: '',
        status: '',
      },
    }
  },
  mounted() {
    this.getInfo()
  },
  methods: {
    // 获取详情
    async getInfo() {
      const info = await cost.getCostInfo(this.costId)
      this.form = {
        name: info.name,
        price: info.price,
        discount: info.discount,
        province_discount: info.province_discount,
        city_discount: info.city_discount,
        county_discount: info.county_discount,
        status: info.status,
      }
    },
    // eslint-disable-next-line no-unused-vars
    async submitForm(formName) {
      const self = this
      // eslint-disable-next-line no-unused-vars
      const info = {
        id: this.costId,
        name: self.form.name,
        price: self.form.price,
        discount: self.form.discount,
        province_discount: self.form.province_discount,
        city_discount: self.form.city_discount,
        county_discount: self.form.county_discount,
        status: self.form.status,
      }
      try {
        // eslint-disable-next-line no-unused-vars
        const res = await cost.updateCost(info)
        if (res.error_code === 0) {
          this.$message.success(`${res.msg}`)
          this.back()
        }
      } catch (error) {
        console.log(error)
      }
    },
    // 重置表单
    resetForm() {
      this.getInfo()
    },
    back() {
      this.$emit('editClose')
    },
  },
}
</script>

<style lang="scss" scoped>
    .el-divider--horizontal {
        margin: 0
    }

    .container {
        .title {
            height: 59px;
            line-height: 59px;
            color: $parent-title-color;
            font-size: 16px;
            font-weight: 500;
            text-indent: 40px;

            .back {
                float: right;
                margin-right: 40px;
                cursor: pointer;
            }
        }

        .wrap {
            padding: 20px;
        }

        .submit {
            float: left;
        }
    }
</style>
